package la.renzhen.rtpt.admin.boot.starter.jdbc;

import com.alibaba.druid.pool.DruidDataSource;
import la.renzhen.rtpt.admin.boot.conditional.ConditionalOnPrefixProperty;
import la.renzhen.rtpt.admin.boot.starter.CenterServerAutoConfigruation;
import la.renzhen.rtpt.admin.source.CenterConfigurationSource;
import la.renzhen.rtpt.admin.source.jdbc.JDBCCenterConfigurationSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * <p>
 *
 * @author <a href="mailto:zhouhaichao@2008.sina.com">haiker</a>
 * @version 16/05/2018 7:29 PM
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
@AutoConfigureBefore(CenterServerAutoConfigruation.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@ConditionalOnMissingBean(CenterConfigurationSource.class)
@ConditionalOnPrefixProperty("rtpt.admin.jdbc")
public class JdbcSourceAutoConfiguration {

    @Bean
    public CenterConfigurationSource jdbcConfig(
            Environment env, JdbcProperties config,
            @Autowired(required = false) DataSource dataSource
    ) throws SQLException
    {
        if (!StringUtils.hasText(config.getUrl())) {
            if (dataSource != null) {
                return new JDBCCenterConfigurationSource(new JdbcTemplate(dataSource));
            } else {
                config.setUrl(env.getProperty("spring.datasource.url"));
                config.setUsername(env.getProperty("spring.datasource.username"));
                config.setPassword(env.getProperty("spring.datasource.password"));
            }
        }

        DruidDataSource druid = new DruidDataSource();
        if (config.getUsername() != null) {
            druid.setUsername(config.getUsername());
        }
        if (config.getPassword() != null) {
            druid.setPassword(config.getPassword());
        }
        if (config.getUrl() != null) {
            druid.setUrl(config.getUrl());
        }
        if (config.getDriverClassName() != null) {
            druid.setDriverClassName(config.getDriverClassName());
        }
        druid.setMaxActive(config.getMaxActive());
        druid.setInitialSize(config.getInitialSize());
        druid.setMaxWait(config.getMaxWait());
        druid.setMinIdle(config.getMinIdle());
        druid.setTimeBetweenEvictionRunsMillis(config.getTimeBetweenEvictionRunsMillis());
        druid.setMinEvictableIdleTimeMillis(config.getMinEvictableIdleTimeMillis());
        druid.setTestWhileIdle(config.isTestWhileIdle());
        druid.setTestOnBorrow(config.isTestOnBorrow());
        druid.setTestOnReturn(config.isTestOnReturn());
        druid.setPoolPreparedStatements(config.isPoolPreparedStatements());
        druid.setMaxOpenPreparedStatements(config.getMaxOpenPreparedStatements());
        druid.init();

        return new JDBCCenterConfigurationSource(new JdbcTemplate(druid));
    }

}
